import json
from fastapi import APIRouter, HTTPException, Query, BackgroundTasks
from entity.response_schema import response_base
from core.bilibili.bilibili_database import create_bilibili_user_db, create_bilibili_main_user_db, create_bilibili_direct_message_db
from core.bilibili.biliMain import select_user, select_main_user_message, select_qrcode, select_qrcode_key, add_task

from pydantic import BaseModel

router = APIRouter()

class Item(BaseModel):
    token: str

@router.get("/qrCode", name="二维码")
async def get_qrCode():
    try:
        create_bilibili_main_user_db()
        create_bilibili_user_db()
        create_bilibili_direct_message_db()
        data = select_qrcode()
        return await response_base.success(data=data['data'])
    except Exception as e:
        # 处理异常，返回错误响应
        return response_base.fail(msg=f"{e}")

@router.get("/poll", name="验证二维码，返回token")
async def get_poll(qrcode_key):
    try:
        data = select_qrcode_key(qrcode_key)
        print(data)
        return await response_base.success(data=data)
    except Exception as e:
        # 处理异常，返回错误响应
        return response_base.fail(msg=f"{e}")


@router.post("/bilibiliMessage", name="加载用户私聊")
async def get_users(item: Item):
    add_task(item)
    return await response_base.success()

@router.post("/myUser", name="获取token用户")
async def my_user(item:Item):
    # 文章ID
    try:
        data = select_main_user_message(item.token)
        return await response_base.success(data = data)
    except Exception as e:
        # 处理异常，返回错误响应
        return response_base.fail(msg=f"{e}")